# -*- coding: utf-8 -*-

import globalvar
import datetime

class   emsdatabase():

    
    def __init__(self):
        pass
    
    def uploadreceivebill(self,all_values_list):
        
        globalvar.socketlogging.debug("upload receive bill")
        
        serial_number = all_values_list[0]
        sender_name = all_values_list[1]
        sender_phone = all_values_list[2]
        bill_count_str = all_values_list[3]        
        bill_count = int(bill_count_str,16)
        
        scan_type = all_values_list[4]

        terminal_code = all_values_list[5].decode('utf-8').encode('gbk')
        destination_code = all_values_list[6].decode('utf-8').encode('gbk')
        
        sms_notify = all_values_list[7]
        n=datetime.datetime.now()
        id_str = n.isoformat(' ')
        
        globalvar.socketlogging.debug("serial_number %s"%(serial_number))
        globalvar.socketlogging.debug("sender_name %s"%(sender_name))
        globalvar.socketlogging.debug("sender_phone %s"%(sender_phone))
        globalvar.socketlogging.debug("scan_type %s"%(scan_type))
        globalvar.socketlogging.debug("sms_notify %s"%(sms_notify))
        globalvar.socketlogging.debug("bill_count_str %s"%(bill_count_str))
        
        

        serial_number = serial_number.decode('utf-8').encode('gbk')
        sender_name = sender_name.decode('utf-8').encode('gbk')
        scan_type = scan_type.decode('utf-8').encode('gbk')
        sms_notify = sms_notify.decode('utf-8').encode('gbk')
        
        for index in range(0,bill_count):
            bill_no = all_values_list[6 + 2 * index]
            globalvar.socketlogging.debug("bill_no %s"%(bill_no))
            bill_no = bill_no.decode('utf-8').encode('gbk')            
            receiver_phone = all_values_list[6 + 2 * index + 1]
            insert_record_str = "insert into TAB_GPRS_SCAN_QY\
             (%s) values\
                ('%s','%s', '321001'  , '456001'    , '001'        ,\
                 '%s'     , ''        , 'scan_man',  'scan_site', sysdate,\
        'dispatchorman', '%s'         , ''        , '22'        , '0'        ,\
                '01'   , '0'       , 'ww001'   , 'q001'      , 'test001',\
                '25'   , '10.00'   , 'signname', 1           , '%s',\
                 '%s' ,%d,'%s','%s')"%(globalvar.TAB_GPRS_SCAN_QY_COLUMN,id_str,bill_no,scan_type,sender_name
                                                                   ,sender_phone,receiver_phone,sms_notify
                                                                   ,terminal_code,destination_code)

            globalvar.socketlogging.debug("insert_record_str = %s"%(insert_record_str))                 
            globalvar.cursor.execute(insert_record_str)          
            globalvar.conn.commit()
            
        update_str = "UPDATE TAB_GPRS_MSG SET MSG_STATE=2  WHERE SERIAL_NUMBER='%s' AND MSG_STATE=1;"%(serial_number)
        globalvar.databaselogging.debug("upate_str = %s"%(update_str))
        globalvar.cursor.execute(update_str)
        globalvar.conn.commit()

            
         
            
    def newreceivebill(self,all_values_list):

        globalvar.socketlogging.debug("new receive bill")

        sender_phone = all_values_list[0]
        scan_type = all_values_list[1]
        terminal_code = all_values_list[2].decode('utf-8').encode('gbk')
        destination_code = all_values_list[3].decode('utf-8').encode('gbk')
        
        sms_notify = all_values_list[4]
        bill_count_str = all_values_list[5]        
        bill_count = int(bill_count_str,16)             

        globalvar.socketlogging.debug("sender_phone %s"%(sender_phone))
        globalvar.socketlogging.debug("scan_type %s"%(scan_type))
        globalvar.socketlogging.debug("sms_notify %s"%(sms_notify))
        globalvar.socketlogging.debug("bill_count_str %s"%(bill_count_str))        

        sender_phone = sender_phone.decode('utf-8').encode('gbk')
        scan_type = scan_type.decode('utf-8').encode('gbk')
        sms_notify = int(sms_notify.decode('utf-8').encode('gbk'),16)
        
        
        for index in range(0,bill_count):
            bill_no = all_values_list[4 + 2 * index + 1]
            bill_no = bill_no.decode('utf-8').encode('gbk')            
            receiver_phone = (all_values_list[4 + 2 * index ])
            globalvar.socketlogging.debug("bill_no %s"%(bill_no))
            globalvar.socketlogging.debug("receiver_phone %s"%(receiver_phone))
            n=datetime.datetime.now()
            id_str = n.isoformat(' ')
            
            insert_record_str = "insert into TAB_GPRS_SCAN_QY\
             (%s) values\
                ('%s','%s', '321001'  , '456001'    , '001'        ,\
                 '%s'     , ''        , 'scan_man',  'scan_site', sysdate,\
        'dispatchorman', 'sendername'         , ''        , '22'        , '0'        ,\
                '01'   , '0'       , 'ww001'   , 'q001'      , 'test001',\
                '25'   , '10.00'   , 'signname', 1           , '%s',\
                 '%s' ,%d)"%(globalvar.TAB_GPRS_SCAN_QY_COLUMN,id_str,bill_no,scan_type,sender_phone,receiver_phone
                                                                             ,sms_notify,terminal_code,destination_code)                 
            globalvar.socketlogging.debug("insert_record_str %s"%(insert_record_str))                 
            globalvar.cursor.execute(insert_record_str)
            globalvar.conn.commit()
                     
            
    def updatereceivebill(self,all_values_list):
        bill_no = all_values_list[0]
        signin_name = all_values_list[1]

        
        
    
